package com.feicheng.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.common.constant.Constant;
import com.feicheng.persistence.entity.VoucherRecord;
import com.feicheng.persistence.service.IVoucherRecordService;
import com.feicheng.service.BaseService;
import com.feicheng.service.IFPOIService;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Service
public class FPOIServiceImpl extends BaseService implements IFPOIService {

    @Autowired
    IVoucherRecordService voucherRecordService;

    @Override
    public void downLoadExcel(HttpServletResponse response,
                              String name,
                              String uuId,
                              Boolean statu,
                              JSONArray recordIds
                              ) {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("卡券");

        QueryWrapper<VoucherRecord> queryWrapper = new QueryWrapper<VoucherRecord>();

        if (StringUtils.hasLength(name)) {
            //根据会员劵名称查询
            queryWrapper.like(Constant.ENTITY.VOUCHER_TYPE.NAME, name);
        }

        if (StringUtils.hasLength(uuId)) {
            //根据卡号查询
            queryWrapper.like(Constant.ENTITY.VOUCHER_RECORD.UU_ID, uuId);
        }

        if (null != recordIds && recordIds.size() > 0) {
            //根据状态查询
            queryWrapper.in(Constant.ENTITY.VOUCHER_RECORD.RECORD_ID, recordIds);
        }

        queryWrapper.eq(Constant.ENTITY.VOUCHER_RECORD.STATU, true);

        List<VoucherRecord> voucherRecords = voucherRecordService.list(queryWrapper);

        String fileName = "voucher_records_export"  + ".xls";//设置要导出的文件的名字

        int rowNum = 1;

        String[] headers = { "UUID", "密码", "时长（天）"};

        HSSFRow row = sheet.createRow(0);

        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        for (VoucherRecord vr : voucherRecords) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(vr.getUuId());
            
            if ("123".equals(vr.getPassword())) {
            	vr.setPassword("无密码");
            }
            
            row1.createCell(1).setCellValue(vr.getPassword());
            row1.createCell(2).setCellValue(vr.getTimeLongs() / 60 / 24);
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        try {
            response.flushBuffer();
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
